home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / CmdDlgTwoParams.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  12.9 KB  |  425 lines  |  [TEXT/KAHL]

  1. /* CmdDlgTwoParams.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "CmdDlgTwoParams.h"
  31. #include "Memory.h"
  32. #include "Screen.h"
  33. #include "EventLoop.h"
  34. #include "Menus.h"
  35. #include "TextEdit.h"
  36. #include "SimpleButton.h"
  37. #include "WrapTextBox.h"
  38. #include "DataMunging.h"
  39. #include "Main.h"
  40. #include "Alert.h"
  41. #include "Numbers.h"
  42.  
  43.  
  44. #define WINXSIZE (350)
  45.  
  46. #define PROMPTX (10)
  47. #define PROMPTY (5)
  48. #define PROMPTWIDTH (WINXSIZE - (2 * PROMPTX))
  49. #define PROMPTHEIGHT (50)
  50.  
  51. #define FIRSTBOXPROMPTX (PROMPTX)
  52. #define FIRSTBOXPROMPTY (PROMPTY + PROMPTHEIGHT + 5)
  53.  
  54. #define FIRSTBOXEDITX (FIRSTBOXPROMPTX + 140)
  55. #define FIRSTBOXEDITY (FIRSTBOXPROMPTY - 3)
  56. #define FIRSTBOXEDITWIDTH (140)
  57. #define FIRSTBOXEDITHEIGHT (21)
  58.  
  59. #define SECONDBOXPROMPTX (FIRSTBOXPROMPTX)
  60. #define SECONDBOXPROMPTY (FIRSTBOXPROMPTY + FIRSTBOXEDITHEIGHT + 3)
  61.  
  62. #define SECONDBOXEDITX (FIRSTBOXEDITX)
  63. #define SECONDBOXEDITY (SECONDBOXPROMPTY - 3)
  64. #define SECONDBOXEDITWIDTH (FIRSTBOXEDITWIDTH)
  65. #define SECONDBOXEDITHEIGHT (FIRSTBOXEDITHEIGHT)
  66.  
  67. #define CANCELBUTTONWIDTH (80)
  68. #define CANCELBUTTONHEIGHT (21)
  69. #define CANCELBUTTONX ((1 * WINXSIZE) / 4 - (CANCELBUTTONWIDTH / 2))
  70. #define CANCELBUTTONY (SECONDBOXEDITY + SECONDBOXEDITHEIGHT + 10)
  71.  
  72. #define OKBUTTONWIDTH (CANCELBUTTONWIDTH)
  73. #define OKBUTTONHEIGHT (CANCELBUTTONHEIGHT)
  74. #define OKBUTTONX ((3 * WINXSIZE) / 4 - (OKBUTTONWIDTH / 2))
  75. #define OKBUTTONY (CANCELBUTTONY)
  76.  
  77. #define WINYSIZE (CANCELBUTTONY + CANCELBUTTONHEIGHT + 20)
  78.  
  79.  
  80. typedef struct
  81.     {
  82.         WinType*                    ScreenID;
  83.         char*                            PromptText;
  84.         char*                            FirstPromptText;
  85.         TextEditRec*            FirstBoxEdit;
  86.         char*                            SecondPromptText;
  87.         TextEditRec*            SecondBoxEdit;
  88.         SimpleButtonRec*    OKButton;
  89.         SimpleButtonRec*    CancelButton;
  90.     } WindowRec;
  91.  
  92.  
  93. static void            RedrawWindow(WindowRec* Window)
  94.     {
  95.         CheckPtrExistence(Window);
  96.         TextEditFullRedraw(Window->FirstBoxEdit);
  97.         TextEditFullRedraw(Window->SecondBoxEdit);
  98.         RedrawSimpleButton(Window->OKButton);
  99.         RedrawSimpleButton(Window->CancelButton);
  100.         SetClipRect(Window->ScreenID,0,0,WINXSIZE,WINYSIZE);
  101.         DrawWrappedTextBox(Window->ScreenID,Window->PromptText,GetScreenFont(),9,
  102.             PROMPTX,PROMPTY,PROMPTWIDTH,PROMPTHEIGHT);
  103.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,Window->FirstPromptText,
  104.             StrLen(Window->FirstPromptText),FIRSTBOXPROMPTX,FIRSTBOXPROMPTY,ePlain);
  105.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,Window->SecondPromptText,
  106.             StrLen(Window->SecondPromptText),SECONDBOXPROMPTX,SECONDBOXPROMPTY,ePlain);
  107.     }
  108.  
  109.  
  110. /* present a dialog box that allows the user to edit two parameters */
  111. /* returns True if the user changes the value and clicks OK. */
  112. MyBoolean                CommandDialogTwoParams(char* Prompt, char* FirstBoxName,
  113.                                     double* FirstDataInOut, char* SecondBoxName, double* SecondDataInOut)
  114.     {
  115.         WindowRec*        Window;
  116.         char*                    StringTemp;
  117.         MyBoolean            LoopFlag;
  118.         MyBoolean            DoItFlag EXECUTE(= -31342);
  119.         MyBoolean            ReturnValue;
  120.         TextEditRec*    ActiveTextEdit;
  121.  
  122.         Window = (WindowRec*)AllocPtrCanFail(sizeof(WindowRec),
  123.             "CommandDialogTwoParams: WindowRec");
  124.         if (Window == NIL)
  125.             {
  126.              FailurePoint1:
  127.                 AlertHalt("There is not enough memory available to edit the "
  128.                     "command parameters.",NIL);
  129.                 return False;
  130.             }
  131.         Window->PromptText = Prompt;
  132.         Window->FirstPromptText = FirstBoxName;
  133.         Window->SecondPromptText = SecondBoxName;
  134.  
  135.         Window->ScreenID = MakeNewWindow(eModelessDialogWindow,eWindowNotClosable,
  136.             eWindowNotZoomable,eWindowNotResizable,DialogLeftEdge(WINXSIZE),
  137.             DialogTopEdge(WINYSIZE),WINXSIZE,WINYSIZE,(void (*)(void*))&RedrawWindow,Window);
  138.         if (Window->ScreenID == NIL)
  139.             {
  140.              FailurePoint2:
  141.                 ReleasePtr((char*)Window);
  142.                 goto FailurePoint1;
  143.             }
  144.         SetWindowName(Window->ScreenID,"Edit Command");
  145.  
  146.         Window->OKButton = NewSimpleButton(Window->ScreenID,"OK",OKBUTTONX,OKBUTTONY,
  147.             OKBUTTONWIDTH,OKBUTTONHEIGHT);
  148.         if (Window->OKButton == NIL)
  149.             {
  150.              FailurePoint3:
  151.                 KillWindow(Window->ScreenID);
  152.                 goto FailurePoint2;
  153.             }
  154.         SetDefaultButtonState(Window->OKButton,True);
  155.  
  156.         Window->CancelButton = NewSimpleButton(Window->ScreenID,"Cancel",CANCELBUTTONX,
  157.             CANCELBUTTONY,CANCELBUTTONWIDTH,CANCELBUTTONHEIGHT);
  158.         if (Window->CancelButton == NIL)
  159.             {
  160.              FailurePoint4:
  161.                 DisposeSimpleButton(Window->OKButton);
  162.                 goto FailurePoint3;
  163.             }
  164.  
  165.         Window->FirstBoxEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,GetScreenFont(),
  166.             9,FIRSTBOXEDITX,FIRSTBOXEDITY,FIRSTBOXEDITWIDTH,FIRSTBOXEDITHEIGHT);
  167.         if (Window->FirstBoxEdit == NIL)
  168.             {
  169.              FailurePoint5:
  170.                 DisposeSimpleButton(Window->CancelButton);
  171.                 goto FailurePoint4;
  172.             }
  173.         StringTemp = LongDoubleToString(*FirstDataInOut,12,1e-6,1e6);
  174.         if (StringTemp == NIL)
  175.             {
  176.              FailurePoint6:
  177.                 DisposeTextEdit(Window->FirstBoxEdit);
  178.                 goto FailurePoint5;
  179.             }
  180.         TextEditNewRawData(Window->FirstBoxEdit,StringTemp,SYSTEMLINEFEED);
  181.         ReleasePtr(StringTemp);
  182.         TextEditHasBeenSaved(Window->FirstBoxEdit);
  183.  
  184.         Window->SecondBoxEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,GetScreenFont(),
  185.             9,SECONDBOXEDITX,SECONDBOXEDITY,SECONDBOXEDITWIDTH,SECONDBOXEDITHEIGHT);
  186.         if (Window->SecondBoxEdit == NIL)
  187.             {
  188.              FailurePoint7:
  189.                 goto FailurePoint6;
  190.             }
  191.         StringTemp = LongDoubleToString(*SecondDataInOut,12,1e-6,1e6);
  192.         if (StringTemp == NIL)
  193.             {
  194.              FailurePoint8:
  195.                 DisposeTextEdit(Window->SecondBoxEdit);
  196.                 goto FailurePoint7;
  197.             }
  198.         TextEditNewRawData(Window->SecondBoxEdit,StringTemp,SYSTEMLINEFEED);
  199.         ReleasePtr(StringTemp);
  200.         TextEditHasBeenSaved(Window->SecondBoxEdit);
  201.  
  202.  
  203.         ActiveTextEdit = Window->FirstBoxEdit;
  204.         EnableTextEditSelection(ActiveTextEdit);
  205.         LoopFlag = True;
  206.         while (LoopFlag)
  207.             {
  208.                 OrdType                            X;
  209.                 OrdType                            Y;
  210.                 ModifierFlags                Modifiers;
  211.                 MenuItemType*                MenuItem;
  212.                 char                                KeyPress;
  213.  
  214.                 switch (GetAnEvent(&X,&Y,&Modifiers,NIL,&MenuItem,&KeyPress))
  215.                     {
  216.                         default:
  217.                             break;
  218.                         case eCheckCursor:
  219.                             if (TextEditIBeamTest(Window->FirstBoxEdit,X,Y)
  220.                                 || TextEditIBeamTest(Window->SecondBoxEdit,X,Y))
  221.                                 {
  222.                                     SetIBeamCursor();
  223.                                 }
  224.                              else
  225.                                 {
  226.                                     SetArrowCursor();
  227.                                 }
  228.                             goto UpdateCursorPoint;
  229.                             break;
  230.                         case eNoEvent:
  231.                          UpdateCursorPoint:
  232.                             TextEditUpdateCursor(ActiveTextEdit);
  233.                             break;
  234.                         case eMenuStarting:
  235.                             EnableMenuItem(mPaste);
  236.                             if (TextEditIsThereValidSelection(ActiveTextEdit))
  237.                                 {
  238.                                     EnableMenuItem(mCut);
  239.                                     EnableMenuItem(mCopy);
  240.                                     EnableMenuItem(mClear);
  241.                                 }
  242.                             EnableMenuItem(mSelectAll);
  243.                             if (TextEditCanWeUndo(ActiveTextEdit))
  244.                                 {
  245.                                     EnableMenuItem(mUndo);
  246.                                 }
  247.                             break;
  248.                         case eMenuCommand:
  249.                             if (MenuItem == mPaste)
  250.                                 {
  251.                                     TextEditDoMenuPaste(ActiveTextEdit);
  252.                                 }
  253.                             else if (MenuItem == mCut)
  254.                                 {
  255.                                     TextEditDoMenuCut(ActiveTextEdit);
  256.                                 }
  257.                             else if (MenuItem == mCopy)
  258.                                 {
  259.                                     TextEditDoMenuCopy(ActiveTextEdit);
  260.                                 }
  261.                             else if (MenuItem == mClear)
  262.                                 {
  263.                                     TextEditDoMenuClear(ActiveTextEdit);
  264.                                 }
  265.                             else if (MenuItem == mUndo)
  266.                                 {
  267.                                     TextEditDoMenuUndo(ActiveTextEdit);
  268.                                     TextEditShowSelection(ActiveTextEdit);
  269.                                 }
  270.                             else if (MenuItem == mSelectAll)
  271.                                 {
  272.                                     TextEditDoMenuSelectAll(ActiveTextEdit);
  273.                                 }
  274.                             else
  275.                                 {
  276.                                     EXECUTE(PRERR(AllowResume,
  277.                                         "CommandDialogTwoParams: Undefined menu option chosen"));
  278.                                 }
  279.                             break;
  280.                         case eKeyPressed:
  281.                             if (KeyPress == 13)
  282.                                 {
  283.                                     FlashButton(Window->OKButton);
  284.                                     DoItFlag = True;
  285.                                     LoopFlag = False;
  286.                                 }
  287.                             else if (KeyPress == 9)
  288.                                 {
  289.                                     DisableTextEditSelection(ActiveTextEdit);
  290.                                     if ((eShiftKey & Modifiers) == 0)
  291.                                         {
  292.                                             /* tab forwards */
  293.                                             if (ActiveTextEdit == Window->FirstBoxEdit)
  294.                                                 {
  295.                                                     ActiveTextEdit = Window->SecondBoxEdit;
  296.                                                 }
  297.                                             else
  298.                                                 {
  299.                                                     ActiveTextEdit = Window->FirstBoxEdit;
  300.                                                 }
  301.                                         }
  302.                                      else
  303.                                         {
  304.                                             /* tab backwards */
  305.                                             if (ActiveTextEdit == Window->SecondBoxEdit)
  306.                                                 {
  307.                                                     ActiveTextEdit = Window->FirstBoxEdit;
  308.                                                 }
  309.                                             else
  310.                                                 {
  311.                                                     ActiveTextEdit = Window->SecondBoxEdit;
  312.                                                 }
  313.                                         }
  314.                                     TextEditDoMenuSelectAll(ActiveTextEdit);
  315.                                     EnableTextEditSelection(ActiveTextEdit);
  316.                                 }
  317.                             else if (KeyPress == eCancelKey)
  318.                                 {
  319.                                     FlashButton(Window->CancelButton);
  320.                                     DoItFlag = False;
  321.                                     LoopFlag = False;
  322.                                 }
  323.                             else
  324.                                 {
  325.                                     TextEditDoKeyPressed(ActiveTextEdit,KeyPress,Modifiers);
  326.                                 }
  327.                             break;
  328.                         case eMouseDown:
  329.                             if (SimpleButtonHitTest(Window->OKButton,X,Y))
  330.                                 {
  331.                                     if (SimpleButtonMouseDown(Window->OKButton,X,Y,NIL,NIL))
  332.                                         {
  333.                                             DoItFlag = True;
  334.                                             LoopFlag = False;
  335.                                         }
  336.                                 }
  337.                             else if (SimpleButtonHitTest(Window->CancelButton,X,Y))
  338.                                 {
  339.                                     if (SimpleButtonMouseDown(Window->CancelButton,X,Y,NIL,NIL))
  340.                                         {
  341.                                             DoItFlag = False;
  342.                                             LoopFlag = False;
  343.                                         }
  344.                                 }
  345.                             else if (TextEditHitTest(Window->FirstBoxEdit,X,Y))
  346.                                 {
  347.                                     if (ActiveTextEdit != Window->FirstBoxEdit)
  348.                                         {
  349.                                             DisableTextEditSelection(ActiveTextEdit);
  350.                                             ActiveTextEdit = Window->FirstBoxEdit;
  351.                                             EnableTextEditSelection(ActiveTextEdit);
  352.                                         }
  353.                                     TextEditDoMouseDown(ActiveTextEdit,X,Y,Modifiers);
  354.                                 }
  355.                             else if (TextEditHitTest(Window->SecondBoxEdit,X,Y))
  356.                                 {
  357.                                     if (ActiveTextEdit != Window->SecondBoxEdit)
  358.                                         {
  359.                                             DisableTextEditSelection(ActiveTextEdit);
  360.                                             ActiveTextEdit = Window->SecondBoxEdit;
  361.                                             EnableTextEditSelection(ActiveTextEdit);
  362.                                         }
  363.                                     TextEditDoMouseDown(ActiveTextEdit,X,Y,Modifiers);
  364.                                 }
  365.                             break;
  366.                     }
  367.             }
  368.         ERROR((DoItFlag != True) && (DoItFlag != False),PRERR(ForceAbort,
  369.             "CommandDialogTwoParams:  DoItFlag is neither true nor false"));
  370.  
  371.         ReturnValue = False;
  372.  
  373.         if (DoItFlag)
  374.             {
  375.                 MyBoolean                    ErrorNoMemory;
  376.  
  377.                 ErrorNoMemory = False;
  378.  
  379.                 if (TextEditDoesItNeedToBeSaved(Window->FirstBoxEdit))
  380.                     {
  381.                         StringTemp = TextEditGetRawData(Window->FirstBoxEdit,SYSTEMLINEFEED);
  382.                         if (StringTemp == NIL)
  383.                             {
  384.                                 ErrorNoMemory = True;
  385.                             }
  386.                          else
  387.                             {
  388.                                 *FirstDataInOut = StringToLongDouble(StringTemp,PtrSize(StringTemp));
  389.                                 ReleasePtr(StringTemp);
  390.                                 ReturnValue = True;
  391.                             }
  392.                     }
  393.  
  394.                 if (TextEditDoesItNeedToBeSaved(Window->SecondBoxEdit))
  395.                     {
  396.                         StringTemp = TextEditGetRawData(Window->SecondBoxEdit,SYSTEMLINEFEED);
  397.                         if (StringTemp == NIL)
  398.                             {
  399.                                 ErrorNoMemory = True;
  400.                             }
  401.                          else
  402.                             {
  403.                                 *SecondDataInOut = StringToLongDouble(StringTemp,PtrSize(StringTemp));
  404.                                 ReleasePtr(StringTemp);
  405.                                 ReturnValue = True;
  406.                             }
  407.                     }
  408.  
  409.                 if (ErrorNoMemory)
  410.                     {
  411.                         AlertHalt("There was not enough memory available to save all of "
  412.                             "the attributes.",NIL);
  413.                     }
  414.             }
  415.  
  416.         DisposeSimpleButton(Window->OKButton);
  417.         DisposeSimpleButton(Window->CancelButton);
  418.         DisposeTextEdit(Window->FirstBoxEdit);
  419.         DisposeTextEdit(Window->SecondBoxEdit);
  420.         KillWindow(Window->ScreenID);
  421.         ReleasePtr((char*)Window);
  422.  
  423.         return ReturnValue;
  424.     }
  425.